'use strict';

const {Controller} = require('egg');

class BaseController extends Controller{
/* 
create --- 新增-post
update --- 编辑-put
detail --- 根据主键进行查询详情-get
destroy --- 删除-delete
page --- 分页查询-get
tree -- 树形列表-get
all --- 所有列表-get
excel --- 导出excel-get

beforeXxx --- 一般用于请求前的参数处理
responseXxx --- 用于结果的处理或回调的其他操作 
*/

    async create(){
        const {ctx,app} = this;
        const params = ctx.request.body;

        const res = await ctx.model[this.modelName].create(params);
        ctx.body = {
            code: 200,
            msg: '创建成功',
            data: res,
        }

    }

    async update(){
        const {ctx,app} = this;
        const params = ctx.request.body;
        let id = params[this.primaryKey];
        const res = await ctx.model[this.modelName].findByPk(id);
        if(!res){
            ctx.body = {
                code: 201,
                msg: '为找到对应的数据',
                data: null,
            }
        }
        await res.update(params);
        ctx.body = {
            code: 200,
            msg: '修改成功',
            data: null
        }
    }

    async detail(){

    }

    async destroy(){

    }

    async page(){

    }

    async all(){

    }

    async tree(){

    }

    async excel(){

    }

}